/********************************************************************************************************************************\
*     Author:RAJUL BHATNAGAR       
*     EMAIL:coolrajulbhatnagar@gmail.com		
*	  About Program:A simple tic tac toe game 
*
*
\**********************************************************************************************************************************/
#include<stdio.h> 
char board[3][3];
const char cross='X';/*Constant cross*/
const char circle='O';/*Constant O*/
char toggle;/*To toggle clearing screen*/
int row;/*Row id for print*/
int	column;/*Column id for print*/
int row_entry;/*Row entry index*/
int column_entry;/*Column entry index*/
int flag=-1;/*Flag to detect weATHER A PERTICULAR BLOCK WAS ENTERED TO TAKE NEXT ACTION*/
int counter_layout2=0;/*lAYOUT 2(NEW)counter to print guide board*/
int match_count=0;/*Counts number of turns played so as to determine match draw.*/
int layout;/*To select which layout.1 OR 2*/
int layout2_entry;/*User input for layout 2*/
int player1_victories=0;//player 2 victories
int player2_victories=0;//player 2 victories
int match_no=1;//Match no selected by user
/*Select the type of layout wanted*/
int layout_select()
{
	printf("Press 1 for the old layout nd intput scheme.(DIFFICULT TO FOLLOW)\nIn this u have to enter the row nd column number of the location.\n\n\n");
	printf("Press 2 for the new layout nd intput scheme.(EASIER TO FOLLOW)\nIn this each location has a number assigned to it.\n");
	scanf("%d",&layout);
	printf("Enter 'y' OR 'Y' to clear screan after each print else press any letter\n");
	fflush(stdout);
	fflush(stdin);
	scanf("%c",&toggle);
	return 0;
}
/*Function to check for victory*/
int check_victory()
{
	if(((board[0][0]==cross) && (board[0][1]==cross) && (board[0][2]==cross))||((board[1][0]==cross) && (board[1][1]==cross) && (board[1][2]==cross))||((board[2][0]==cross) && (board[2][1]==cross) && (board[2][2]==cross))||((board[0][0]==cross) && (board[1][1]==cross) && (board[2][2]==cross))||((board[0][2]==cross) && (board[1][1]==cross) && (board[2][0]== cross))||((board[0][0]==cross) && (board[1][0]==cross) && (board[2][0]==cross))||((board[0][1]==cross) && (board[1][1]==cross) && (board[2][1]==cross))||((board[0][2]==cross) && (board[1][2]==cross) && (board[2][2]==cross)))
	{	
		printf("Player1 WINS!!!!!!!!");
		match_no--;
		player1_victories++;
		flag=1;
	}
	else if(((board[0][0]==circle) && (board[0][1]==circle) && (board[0][2]==circle))||((board[1][0]==circle) && (board[1][1]==circle) && (board[1][2]==circle))||((board[2][0]== circle) && (board[2][1]==circle) && (board[2][2]==circle))||((board[0][0]==circle) && (board[1][1]==circle) && (board[2][2]==circle))||((board[0][2]==circle) && (board[1][1]==circle) && (board[2][0]==circle))||((board[0][0]==circle) && (board[1][0]==circle) && (board[2][0]==circle))||((board[0][1]==circle) && (board[1][1]==circle) && (board[2][1]==circle))||((board[0][2]==circle) && (board[1][2]==circle) && (board[2][2]==circle)))
	{
		printf("Player2 WINS!!!!!!!!");
		player2_victories++;
		match_no--;
		flag=2;
	}
	else if(match_count==9)
		{
			printf("Match drawn");
			match_no--;
			flag=3;
		}	
	return 0;
}
/*Function to print the current state of the board as well as expexted layout Guide board*/
int print_board()
{
	switch(layout)
	{
	case 1:
		{
			printf("Layout nd expected input\n");
			for(row=1;row<=3;row++)
			{
				for(column=1;column<=3;column++)
				{
					printf("%d%d ",row,column);					
				}
					printf("\n");
			}
			printf("Output\n");
			for(row=1;row<=3;row++)
			{
				for(column=1;column<=3;column++)
				{
					printf("%c",board[row-1][column-1]);					
				}
					printf("\n");
			}	
			break;
		}
	case 2:
		{
			printf("Layout nd expected input 1\n");
			for(row=1;row<=3;row++)
			{
				for(column=1;column<=3;column++)
				{
					counter_layout2++;
					printf("%d",counter_layout2);					
				}
					printf("\n");
			}
			counter_layout2=0;
			printf("Output\n");
			for(row=1;row<=3;row++)
			{
				for(column=1;column<=3;column++)
				{
					printf("%c",board[row-1][column-1]);					
				}
					printf("\n");
			}	
			break;
	}
	default :
		{
		printf("Invalid");
		break;
		}
	}
	return 0;
}
/*Function to take player 1 input*/
int player1_input()
{
	switch(layout)
	{
	case 1:
		{
		cont_p1:;
		printf("Enter the row player 1(cross) ");
		scanf("%d",&row_entry);
		fflush(stdout);
		printf("Enter the column player 1(cross) ");
		scanf("%d",&column_entry);
		if(row_entry>3||row_entry<0||column_entry>3||column_entry<0)
		{
			printf("Invalid Entry.Try Again \n");
			goto cont_p1;
		}
		fflush(stdin);
		fflush(stdout);
		if(board[row_entry-1][column_entry-1]=='X'||board[row_entry-1][column_entry-1]=='O')
		{
			printf("Location already filled \n");
			goto cont_p1;
		}
		else
		board[row_entry-1][column_entry-1]=cross;
		match_count++;
		if(toggle=='y'||toggle=='Y')
			system("cls");
		break;
		}
	case 2:
		{
			cont_p1_2:;
			printf("Enter the slot you want to fill as given in the layout");
			scanf("%d",&layout2_entry);
			fflush(stdout);
			switch(layout2_entry)
			{
			case 1:
				{
					row_entry=0;
					column_entry=0;
					break;
				}
			case 2:
				{
					row_entry=0;
					column_entry=1;
					break;
				}
			case 3:
				{
					row_entry=0;
					column_entry=2;
					break;
				}
			case 4:
				{
					row_entry=1;
					column_entry=0;
					break;
				}
			case 5:
				{
					row_entry=1;
					column_entry=1;
					break;
				}
			case 6:
				{
					row_entry=1;
					column_entry=2;
					break;
				}
			case 7:
				{
					row_entry=2;
					column_entry=0;
					break;
				}
			case 8:
				{
					row_entry=2;
					column_entry=1;
					break;
				}
			case 9:
				{
					row_entry=2;
					column_entry=2;
					break;
				}
			default :
				{
					printf("Invalid entry\n");
					goto cont_p1_2;
				}
			}
			fflush(stdin);
			fflush(stdout);
			if(board[row_entry][column_entry]=='X'||board[row_entry][column_entry]=='O')
			{
				printf("Location already filled \n");
				goto cont_p1_2;
			}
			else
			board[row_entry][column_entry]=cross;
			match_count++;
			if(toggle=='y'||toggle=='Y')
				system("cls");
			break;
		}
		}
		
		return 0;
}
/*Function to take player 2 input*/
int player2_input()
{
		switch(layout)
	{
	case 1:
		{
		cont_p2:;
		printf("Enter the row player 2(circle) ");
		scanf("%d",&row_entry);
		fflush(stdout);
		printf("Enter the column player 2(circle) ");
		scanf("%d",&column_entry);
		if(row_entry>3||row_entry<0||column_entry>3||column_entry<0)
		{
			printf("Invalid Entry.Try Again \n");
			goto cont_p2;
		}
		fflush(stdin);
		fflush(stdout);
		if(board[row_entry-1][column_entry-1]=='X'||board[row_entry-1][column_entry-1]=='O')
		{
			printf("Location already filled \n");
			goto cont_p2;
		}
		else
		board[row_entry-1][column_entry-1]=circle;
		match_count++;
		if(toggle=='y'||toggle=='Y')
			system("cls");
		break;
		}
	case 2:
		{
			cont_p2_2:;
			printf("Enter the slot you want to fill as given in the layout");
			scanf("%d",&layout2_entry);
			fflush(stdout);
			switch(layout2_entry)
			{
			case 1:
				{
					row_entry=0;
					column_entry=0;
					break;
				}
			case 2:
				{
					row_entry=0;
					column_entry=1;
					break;
				}
			case 3:
				{
					row_entry=0;
					column_entry=2;
				}
			case 4:
				{
					row_entry=1;
					column_entry=0;
					break;
				}
			case 5:
				{
					row_entry=1;
					column_entry=1;
					break;
				}
			case 6:
				{
					row_entry=1;
					column_entry=2;
					break;
				}
			case 7:
				{
					row_entry=2;
					column_entry=0;
					break;
				}
			case 8:
				{
					row_entry=2;
					column_entry=1;
					break;
				}
			case 9:
				{
					row_entry=2;
					column_entry=2;
					break;
				}
			default :
				{
					printf("Invalid entry\n");
					goto cont_p2_2;
				}
			}
			fflush(stdin);
			fflush(stdout);
			if(board[row_entry][column_entry]=='X'||board[row_entry][column_entry]=='O')
			{
				printf("Location already filled \n");
				goto cont_p2_2;
			}
			else
			board[row_entry][column_entry]=circle;
			match_count++;
			if(toggle=='y'||toggle=='Y')
			system("cls");
			break;
		}
		}
		
		return 0;
}
/*My main function*/
int main()
{
	printf("Enter the number of matches you would like to play\n");
	scanf("%d",&match_no);
	while(match_no>0)
	{
		for(row=1;row<=3;row++)
		{
			for(column=1;column<=3;column++)
				board[row-1][column-1]='_';
		}
		layout_select();
		while(1)
		{
			print_board();
			player1_input();
			print_board();
			check_victory();
			if(flag==1||flag==3)
				break;
			player2_input();
			print_board();
			check_victory();
			if(flag==2||flag==3)
				break;		
		}
		flag=0;
		match_count=0;
		printf("\n");
		printf("Score\tPlayer1 %d Player2 %d\n",player1_victories,player2_victories);
	}
	if(player1_victories>player2_victories)
		printf("Player 1 has won the series %d to %d\n",player1_victories,player2_victories);
	else if(player2_victories>player1_victories)
		printf("Player 2 has won the series %d to %d\n",player2_victories,player1_victories);
	else
		printf("The series was a %d - %d draw\n",player2_victories,player1_victories);
	system("pause");
	return 0;
}
	
